import 'package:flutter/material.dart';

void main(List<String> args) {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    var body = MyBody();
    return MaterialApp(
      title: '我的',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(
            seedColor: const Color.fromARGB(255, 255, 2, 2)),
        useMaterial3: true,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: const Text(
            '主页',
            style: TextStyle(fontSize: 18),
          ),
        ),
        body: body,
        // 浮动按钮
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            print("这是外面的按钮");
          },
          child: Icon(Icons.people),
        ),
      ),
    );
  }
}

class MyBody extends StatelessWidget {
  const MyBody({super.key});

  @override
  Widget build(BuildContext context) {
    return CountDemo();
  }
}

// 有状态组件
class CountDemo extends StatefulWidget {
  const CountDemo({super.key});

  @override
  State<CountDemo> createState() => _CountDemoState();
}

class _CountDemoState extends State<CountDemo> {
  int _number = 0;

  addNumber() {
    setState(() {
      _number++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        // 在上级组件居中
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          // 计数框
          Text(
            "$_number",
            style: Theme.of(context).textTheme.headlineLarge,
          ),
          SizedBox(
            height: 30,
          ),
          // 按钮
          Wrap(
            spacing: 20,
            children: [
              ElevatedButton(
                onPressed: () {
                  setState(() {
                    _number++;
                  });
                },
                child: const Text("+"),
              ),
              ElevatedButton(
                onPressed: () {
                  setState(() {
                    _number--;
                  });
                },
                child: const Text("-"),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
